module klang.ir.工具.IR工具_;
import 全局;
import klang.编译.核心.核心_: 整数_;
import 核心 = klang.编译.核心.核心_;
import klang.编译.语法节点.节点_: 基础节点_ = 节点_;
import klang.ir.类型.IR类型_: ISK, LinkageTypes, AtomicRMWInstBinOp_, InstructionKind_, CmpInst_Predicate_, FastMathFlags_, CallingConv_, TailCallKind_, AtomicOrdering_;
import klang.ir.语法节点.IR节点_: 节点_ = IR节点_, IR数字字面量_, CC调用约定设置_;
LinkageTypes 获取连接类型(节点_ 连接节点 = ··null!(节点_)) {
    if (连接节点 is ··null!(节点_)) {
        return LinkageTypes.无效值_;
    }
    switch (连接节点.目标种类) {
        case ISK.private_: {
            return LinkageTypes.PrivateLinkage;
        }
        case ISK.internal: {
            return LinkageTypes.InternalLinkage;
        }
        case ISK.weak: {
            return LinkageTypes.WeakAnyLinkage;
        }
        case ISK.weak_odr: {
            return LinkageTypes.WeakODRLinkage;
        }
        case ISK.linkonce: {
            return LinkageTypes.LinkOnceAnyLinkage;
        }
        case ISK.linkonce_odr: {
            return LinkageTypes.LinkOnceODRLinkage;
        }
        case ISK.available_externally: {
            return LinkageTypes.AvailableExternallyLinkage;
        }
        case ISK.appending: {
            return LinkageTypes.AppendingLinkage;
        }
        case ISK.common: {
            return LinkageTypes.CommonLinkage;
        }
        case ISK.extern_weak: {
            return LinkageTypes.ExternalWeakLinkage;
        }
        case ISK.external: {
            return LinkageTypes.ExternalLinkage;
        }
        default: {
            assert(false, "不可及"c);
        }
    }
    return LinkageTypes.无效值_;
}
ISK 获取链接特性的令牌(LinkageTypes link) {
    switch (link) {
        case LinkageTypes.PrivateLinkage: {
            return ISK.private_;
        }
        case LinkageTypes.InternalLinkage: {
            return ISK.internal;
        }
        case LinkageTypes.WeakAnyLinkage: {
            return ISK.weak;
        }
        case LinkageTypes.WeakODRLinkage: {
            return ISK.weak_odr;
        }
        case LinkageTypes.LinkOnceAnyLinkage: {
            return ISK.linkonce;
        }
        case LinkageTypes.LinkOnceODRLinkage: {
            return ISK.linkonce_odr;
        }
        case LinkageTypes.AvailableExternallyLinkage: {
            return ISK.available_externally;
        }
        case LinkageTypes.AppendingLinkage: {
            return ISK.appending;
        }
        case LinkageTypes.CommonLinkage: {
            return ISK.common;
        }
        case LinkageTypes.ExternalWeakLinkage: {
            return ISK.extern_weak;
        }
        case LinkageTypes.ExternalLinkage: {
            return ISK.external;
        }
        default: break;
    }
    return ISK.未知__;
}
AtomicRMWInstBinOp_ ISK转为AtomicRMWInstBinOp(节点_ n) {
    switch (n.目标种类) {
        case ISK.xchg: {
            return AtomicRMWInstBinOp_.Xchg;
        }
        case ISK.add: {
            return AtomicRMWInstBinOp_.Add;
        }
        case ISK.sub: {
            return AtomicRMWInstBinOp_.Sub;
        }
        case ISK.and: {
            return AtomicRMWInstBinOp_.And;
        }
        case ISK.nand: {
            return AtomicRMWInstBinOp_.Nand;
        }
        case ISK.or: {
            return AtomicRMWInstBinOp_.Or;
        }
        case ISK.xor: {
            return AtomicRMWInstBinOp_.Xor;
        }
        case ISK.max: {
            return AtomicRMWInstBinOp_.Max;
        }
        case ISK.min: {
            return AtomicRMWInstBinOp_.Min;
        }
        case ISK.umax: {
            return AtomicRMWInstBinOp_.UMax;
        }
        case ISK.umin: {
            return AtomicRMWInstBinOp_.UMin;
        }
        case ISK.fadd: {
            return AtomicRMWInstBinOp_.FAdd;
        }
        case ISK.fsub: {
            return AtomicRMWInstBinOp_.FSub;
        }
        default: break;
    }
    assert(false, "不可及"c);
    return AtomicRMWInstBinOp_.Xchg;
}
InstructionKind_ ISK转为CastOpc(long isk) {
    switch (isk) {
        case ISK.trunc: {
            return InstructionKind_.Trunc;
        }
        case ISK.zext: {
            return InstructionKind_.ZExt;
        }
        case ISK.sext: {
            return InstructionKind_.SExt;
        }
        case ISK.fptrunc: {
            return InstructionKind_.FPTrunc;
        }
        case ISK.fpext: {
            return InstructionKind_.FPExt;
        }
        case ISK.bitcast: {
            return InstructionKind_.BitCast;
        }
        case ISK.addrspacecast: {
            return InstructionKind_.AddrSpaceCast;
        }
        case ISK.uitofp: {
            return InstructionKind_.UIToFP;
        }
        case ISK.sitofp: {
            return InstructionKind_.SIToFP;
        }
        case ISK.fptoui: {
            return InstructionKind_.FPToUI;
        }
        case ISK.fptosi: {
            return InstructionKind_.FPToSI;
        }
        case ISK.inttoptr: {
            return InstructionKind_.IntToPtr;
        }
        case ISK.ptrtoint: {
            return InstructionKind_.PtrToInt;
        }
        default: break;
    }
    return InstructionKind_.无效值_;
}
CmpInst_Predicate_ ISK转为CmpInst_Predicate(long isk) {
    switch (isk) {
        case ISK.oeq: {
            return CmpInst_Predicate_.FCMP_OEQ;
        }
        case ISK.one: {
            return CmpInst_Predicate_.FCMP_ONE;
        }
        case ISK.olt: {
            return CmpInst_Predicate_.FCMP_OLT;
        }
        case ISK.ogt: {
            return CmpInst_Predicate_.FCMP_OGT;
        }
        case ISK.ole: {
            return CmpInst_Predicate_.FCMP_OLE;
        }
        case ISK.oge: {
            return CmpInst_Predicate_.FCMP_OGE;
        }
        case ISK.ord: {
            return CmpInst_Predicate_.FCMP_ORD;
        }
        case ISK.uno: {
            return CmpInst_Predicate_.FCMP_UNO;
        }
        case ISK.ueq: {
            return CmpInst_Predicate_.FCMP_UEQ;
        }
        case ISK.une: {
            return CmpInst_Predicate_.ICMP_UGE;
        }
        case ISK.ult: {
            return CmpInst_Predicate_.ICMP_ULT;
        }
        case ISK.ugt: {
            return CmpInst_Predicate_.ICMP_UGT;
        }
        case ISK.ule: {
            return CmpInst_Predicate_.ICMP_ULE;
        }
        case ISK.uge: {
            return CmpInst_Predicate_.ICMP_UGE;
        }
        case ISK.true_: {
            return CmpInst_Predicate_.FCMP_TRUE;
        }
        case ISK.false_: {
            return CmpInst_Predicate_.FCMP_FALSE;
        }
        case ISK.eq: {
            return CmpInst_Predicate_.ICMP_EQ;
        }
        case ISK.ne: {
            return CmpInst_Predicate_.ICMP_NE;
        }
        case ISK.slt: {
            return CmpInst_Predicate_.ICMP_SLT;
        }
        case ISK.sgt: {
            return CmpInst_Predicate_.ICMP_SGT;
        }
        case ISK.sle: {
            return CmpInst_Predicate_.ICMP_SLE;
        }
        case ISK.sge: {
            return CmpInst_Predicate_.ICMP_SGE;
        }
        default: break;
    }
    assert(false, "不可及"c);
    return CmpInst_Predicate_.无效值_;
}
InstructionKind_ ISK转为比较运算符(long isk) {
    switch (isk) {
        case ISK.icmp: {
            return InstructionKind_.ICmp;
        }
        case ISK.fcmp: {
            return InstructionKind_.FCmp;
        }
        default: break;
    }
    return InstructionKind_.无效值_;
}
InstructionKind_ ISK转为二元运算指令(long isk) {
    switch (isk) {
        case ISK.add: {
            return InstructionKind_.Add;
        }
        case ISK.fadd: {
            return InstructionKind_.FAdd;
        }
        case ISK.sub: {
            return InstructionKind_.Sub;
        }
        case ISK.fsub: {
            return InstructionKind_.FSub;
        }
        case ISK.mul: {
            return InstructionKind_.Mul;
        }
        case ISK.fmul: {
            return InstructionKind_.FMul;
        }
        case ISK.udiv: {
            return InstructionKind_.UDiv;
        }
        case ISK.sdiv: {
            return InstructionKind_.SDiv;
        }
        case ISK.fdiv: {
            return InstructionKind_.FDiv;
        }
        case ISK.urem: {
            return InstructionKind_.URem;
        }
        case ISK.srem: {
            return InstructionKind_.SRem;
        }
        case ISK.frem: {
            return InstructionKind_.FRem;
        }
        case ISK.shl: {
            return InstructionKind_.Shl;
        }
        case ISK.lshr: {
            return InstructionKind_.LShr;
        }
        case ISK.ashr: {
            return InstructionKind_.AShr;
        }
        case ISK.and: {
            return InstructionKind_.And;
        }
        case ISK.or: {
            return InstructionKind_.Or;
        }
        case ISK.xor: {
            return InstructionKind_.Xor;
        }
        default: break;
    }
    return InstructionKind_.无效值_;
}
FastMathFlags_ ISK转为快速数学标志(long isk) {
    switch (isk) {
        case ISK.fast: {
            return ~FastMathFlags_.无效值_;
        }
        case ISK.nnan: {
            return FastMathFlags_.NoNaNs;
        }
        case ISK.ninf: {
            return FastMathFlags_.NoInfs;
        }
        case ISK.nsz: {
            return FastMathFlags_.NoSignedZeros;
        }
        case ISK.arcp: {
            return FastMathFlags_.AllowReciprocal;
        }
        case ISK.contract: {
            return FastMathFlags_.AllowContract;
        }
        case ISK.reassoc: {
            return FastMathFlags_.AllowReassoc;
        }
        case ISK.afn: {
            return FastMathFlags_.ApproxFunc;
        }
        default: break;
    }
    return FastMathFlags_.无效值_;
}
InstructionKind_ ISK转为逻辑运算指令(long isk) {
    switch (isk) {
        case ISK.and: {
            return InstructionKind_.And;
        }
        case ISK.or: {
            return InstructionKind_.Or;
        }
        case ISK.xor: {
            return InstructionKind_.Xor;
        }
        default: break;
    }
    return InstructionKind_.无效值_;
}
CallingConv_ ISK转换调用约定(节点_ n = ··null!(节点_)) {
    if (n is ··null!(节点_)) {
        return CallingConv_.C;
    }
    switch (n.目标种类) {
        case ISK.CC调用约定设置: {
            uint 值 = (cast(uint)((cast(IR数字字面量_)((cast(CC调用约定设置_)(n)).值)).整数值));
            return 核心.bitTo!(CallingConv_, uint)(值);
        }
        case ISK.ccc: {
            return CallingConv_.C;
        }
        case ISK.fastcc: {
            return CallingConv_.Fast;
        }
        case ISK.coldcc: {
            return CallingConv_.Cold;
        }
        case ISK.cfguard_checkcc: {
            return CallingConv_.CFGuard_Check;
        }
        case ISK.x86_stdcallcc: {
            return CallingConv_.X86_StdCall;
        }
        case ISK.x86_fastcallcc: {
            return CallingConv_.X86_FastCall;
        }
        case ISK.x86_regcallcc: {
            return CallingConv_.X86_RegCall;
        }
        case ISK.x86_thiscallcc: {
            return CallingConv_.X86_ThisCall;
        }
        case ISK.x86_vectorcallcc: {
            return CallingConv_.X86_VectorCall;
        }
        case ISK.arm_apcscc: {
            return CallingConv_.ARM_APCS;
        }
        case ISK.arm_aapcscc: {
            return CallingConv_.ARM_AAPCS;
        }
        case ISK.arm_aapcs_vfpcc: {
            return CallingConv_.ARM_AAPCS_VFP;
        }
        case ISK.aarch64_vector_pcs: {
            return CallingConv_.AArch64_VectorCall;
        }
        case ISK.aarch64_sve_vector_pcs: {
            return CallingConv_.AArch64_SVE_VectorCall;
        }
        case ISK.msp430_intrcc: {
            return CallingConv_.MSP430_INTR;
        }
        case ISK.avr_intrcc: {
            return CallingConv_.AVR_INTR;
        }
        case ISK.avr_signalcc: {
            return CallingConv_.AVR_SIGNAL;
        }
        case ISK.ptx_kernel: {
            return CallingConv_.PTX_Kernel;
        }
        case ISK.ptx_device: {
            return CallingConv_.PTX_Device;
        }
        case ISK.spir_kernel: {
            return CallingConv_.SPIR_KERNEL;
        }
        case ISK.spir_func: {
            return CallingConv_.SPIR_FUNC;
        }
        case ISK.intel_ocl_bicc: {
            return CallingConv_.Intel_OCL_BI;
        }
        case ISK.x86_64_sysvcc: {
            return CallingConv_.X86_64_SysV;
        }
        case ISK.win64cc: {
            return CallingConv_.Win64;
        }
        case ISK.webkit_jscc: {
            return CallingConv_.WebKit_JS;
        }
        case ISK.anyregcc: {
            return CallingConv_.AnyReg;
        }
        case ISK.preserve_mostcc: {
            return CallingConv_.PreserveMost;
        }
        case ISK.preserve_allcc: {
            return CallingConv_.PreserveAll;
        }
        case ISK.ghccc: {
            return CallingConv_.GHC;
        }
        case ISK.swiftcc: {
            return CallingConv_.Swift;
        }
        case ISK.swifttailcc: {
            return CallingConv_.SwiftTail;
        }
        case ISK.x86_intrcc: {
            return CallingConv_.X86_INTR;
        }
        case ISK.hhvmcc: {
            return CallingConv_.HHVM;
        }
        case ISK.hhvm_ccc: {
            return CallingConv_.HHVM_C;
        }
        case ISK.cxx_fast_tlscc: {
            return CallingConv_.CXX_FAST_TLS;
        }
        case ISK.amdgpu_vs: {
            return CallingConv_.AMDGPU_VS;
        }
        case ISK.amdgpu_gfx: {
            return CallingConv_.AMDGPU_Gfx;
        }
        case ISK.amdgpu_ls: {
            return CallingConv_.AMDGPU_LS;
        }
        case ISK.amdgpu_hs: {
            return CallingConv_.AMDGPU_HS;
        }
        case ISK.amdgpu_es: {
            return CallingConv_.AMDGPU_ES;
        }
        case ISK.amdgpu_gs: {
            return CallingConv_.AMDGPU_GS;
        }
        case ISK.amdgpu_ps: {
            return CallingConv_.AMDGPU_PS;
        }
        case ISK.amdgpu_cs: {
            return CallingConv_.AMDGPU_CS;
        }
        case ISK.amdgpu_kernel: {
            return CallingConv_.AMDGPU_KERNEL;
        }
        case ISK.tailcc: {
            return CallingConv_.Tail;
        }
        default: {
            return CallingConv_.C;
        }
    }
}
TailCallKind_ ISK转尾调用标志(节点_ n = ··null!(节点_)) {
    if (n is ··null!(节点_)) {
        return TailCallKind_.TCK_None;
    }
    switch (n.目标种类) {
        case ISK.musttail: {
            return TailCallKind_.TCK_MustTail;
        }
        case ISK.tail: {
            return TailCallKind_.TCK_Tail;
        }
        case ISK.notail: {
            return TailCallKind_.TCK_NoTail;
        }
        default: break;
    }
    return TailCallKind_.TCK_None;
}
AtomicOrdering_ ISK转AtomicOrdering(节点_ n = ··null!(节点_)) {
    if (n is ··null!(节点_)) {
        return AtomicOrdering_.NotAtomic;
    }
    switch (n.目标种类) {
        case ISK.unordered: {
            return AtomicOrdering_.Unordered;
        }
        case ISK.monotonic: {
            return AtomicOrdering_.Acquire;
        }
        case ISK.acquire: {
            return AtomicOrdering_.Acquire;
        }
        case ISK.release: {
            return AtomicOrdering_.Release;
        }
        case ISK.acq_rel: {
            return AtomicOrdering_.AcquireRelease;
        }
        case ISK.seq_cst: {
            return AtomicOrdering_.SequentiallyConsistent;
        }
        default: break;
    }
    return AtomicOrdering_.NotAtomic;
}
dstring getAtomicRMWInstBinOpName(节点_ n) {
    switch (n.目标种类) {
        case ISK.xchg: {
            return "xchg"d;
        }
        case ISK.add: {
            return "add"d;
        }
        case ISK.sub: {
            return "sub"d;
        }
        case ISK.and: {
            return "and"d;
        }
        case ISK.nand: {
            return "nand"d;
        }
        case ISK.or: {
            return "or"d;
        }
        case ISK.xor: {
            return "xor"d;
        }
        case ISK.max: {
            return "max"d;
        }
        case ISK.min: {
            return "min"d;
        }
        case ISK.umax: {
            return "umax"d;
        }
        case ISK.umin: {
            return "umin"d;
        }
        case ISK.fadd: {
            return "fadd"d;
        }
        case ISK.fsub: {
            return "fsub"d;
        }
        default: break;
    }
    assert(false, "不可及"c);
    return ""d;
}
